package net.istore.market.web;

import java.util.Map;

import net.istore.market.domain.Order;
import net.istore.market.service.order.OrderService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class OrderController {

	@Autowired
	private OrderService orderService;

	@RequestMapping("/index")
	public String listContacts(Map<String, Object> map) {

		map.put("order", new Order());
		map.put("orderList", orderService.getOrders());

		return "order";
	}
	
	@RequestMapping("/")
	public String home() {
		return "redirect:/index";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addContact(@ModelAttribute("order") Order order,
			BindingResult result) {

        orderService.addOrder(order);

		return "redirect:/index";
	}

	@RequestMapping("/delete/{orderId}")
	public String deleteContact(@PathVariable("orderId") Integer orderId) {

        orderService.removeOrder(orderId);

		return "redirect:/index";
	}
}
